﻿
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tetris.Interfaces;

namespace Tetris.Blocks
{
    // ReSharper disable InconsistentNaming
    public class MirroredZBlock : AbstractBlock
    // ReSharper restore InconsistentNaming
    {
        // _-
        private readonly ITile _tileTopRight;
        private readonly ITile _tileTop;
        private readonly ITile _tileBottom;
        private readonly ITile _tileBottomLeft;

        public MirroredZBlock(Texture2D texture, Color color, Vector2 position, int fallDelay)
        {
            Texture = texture;
            Color = color;
            Tiles = new List<ITile>();

            // setup skeleton
            _tileTopRight = new Tile(Texture, Color);
            _tileTop = new Tile(Texture, Color);
            _tileBottom = new Tile(Texture, Color);
            _tileBottomLeft = new Tile(Texture, Color);
            Tiles.AddRange(new[] { _tileTopRight, _tileTop, _tileBottom, _tileBottomLeft });

            Position = new Vector2(position.X, position.Y);
            FallDelay = fallDelay;
            LastFallen = 0;
        }

        protected override void ReArrangeParts(Vector2 position, RotationAngle angle)
        {
            if (angle == RotationAngle.South || angle == RotationAngle.North)
            {
                _tileTopRight.Position = new Vector2(position.X + Texture.Width+Texture.Width/2, position.Y-Texture.Height/2);
                _tileTop.Position = new Vector2(position.X + Texture.Width / 2, position.Y - Texture.Height / 2);
                _tileBottom.Position = new Vector2(position.X + Texture.Width / 2, position.Y + Texture.Height / 2);
                _tileBottomLeft.Position = new Vector2(position.X-Texture.Width/2, position.Y + Texture.Height/2);
            }
            else
            {
                _tileTopRight.Position = new Vector2(position.X-Texture.Width/2, position.Y - Texture.Height - Texture.Height/2);
                _tileTop.Position = new Vector2(position.X - Texture.Width / 2, position.Y - Texture.Height / 2);
                _tileBottom.Position = new Vector2(position.X + Texture.Width / 2, position.Y - Texture.Height / 2);
                _tileBottomLeft.Position = new Vector2(position.X + Texture.Width / 2, position.Y + Texture.Height / 2);
            }
        }

        public override void Rotate()
        {
            CurrentAngle = GetNextRotationAngle();
            ReArrangeParts(Position, CurrentAngle);
        }
    }
}
